require('v8-compile-cache')
const path = require('path');
const { loaders, plugins, Core } = require('./pure-core/index')
const Webpack = require('webpack');
const WebpackDevServer = require('webpack-dev-server');
const __define__ = require('./defineConfig')
const webpackChain = require('webpack-chain');
const chainConfig = new webpackChain();
const notifier = require('node-notifier');
const TerserPlugin = require("terser-webpack-plugin");

new Core()

if (__define__.chainWebpack) {
  __define__.chainWebpack(chainConfig,)
}


const webpackDefaultConfig = {
  // mode: process.env.NODE_ENV,
  // mode: 'production',
  mode: 'development',
  devtool: 'source-map',
  entry: ['@babel/polyfill', path.join(__dirname, "./public/app.js")],
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: '[name].bundle.js',
    clean: true,
  },
  stats: {
    assets: false, // 添加资源信息
    chunks: false,
    // builtAt: false,
    // cached: false,
    // chunkModules: false,
    // depth: false,
    // chunkOrigins: false,
    // entrypoints: false,    errors: true,
    // errorDetails: true,
    children: true,
    modules: false,
    logging: 'warn'
  },
  cache: {
    type: 'memory'
  },
  module: {
    rules: loaders
  },
  plugins: plugins,
  resolve: {
    extensions: [".js", ".jsx", ".ts", ".tsx", ".json", ".webpack.js", ".web.js"],
    alias: {
      '@': path.resolve(__dirname, 'src/'),
      '~': process.cwd()
    }
  },
  devServer: {
    port: 3000,
    open: true, // 自动打开浏览器
    compress: true, // 启动gzip压缩
    client: {
      logging: 'error', //控制台日志
      overlay: { //浏览器中全屏显示
        errors: true,
        warnings: false,
      },
      progress: true, //浏览器中以百分比显示编译进度
    },
  },
  optimization: {
    // minimize: true,
    // minimizer: [
    //   new TerserPlugin({
    //     minify: TerserPlugin.esbuildMinify,
    //     terserOptions: {},
    //   }),
    // ],
    // usedExports: true,
  },
}


// module.exports = chainConfig.toConfig()
module.exports = (env, argv) => {
  if (process.env.NODE_ENV === 'development') {

  }

  switch (__define__) {
    case __define__.devtool:
      webpackDefaultConfig.devtool = __define__.devtool

    case __define__.hash:
      webpackDefaultConfig.output.filename = '[name].[contenthash].bundle.js'

    case __define__.alias:
      const alias = webpackDefaultConfig.resolve.alias
      webpackDefaultConfig.resolve.alias = Object.assign(
        ...__define__.alias,
        ...alias
      )

    case __define__.proxy:
      webpackDefaultConfig.devServer.proxy = __define__.proxy

    case __define__.devServer:
      webpackDefaultConfig.devServer = Object.assign({
        ...webpackDefaultConfig.devServer,
        ...__define__.devServer,
        proxy: __define__.proxy ?? false
      })

    default:
      null
  }


  return webpackDefaultConfig
}